package pr.iceworld.fernando.gatecommon.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import pr.iceworld.fernando.gatecommon.service.impl.BizService;
import pr.iceworld.fernando.gatecommon.web.result.WebBaseResult;

/**
 * Created by ferna on 2018/6/12.
 */
public abstract class BaseController<Biz extends BizService, T> {
    @Autowired
    protected Biz biz;

    @PostMapping("")
    @ResponseBody
    protected WebBaseResult<T> add(T t){
        biz.insertSelective(t);
        return new WebBaseResult<T>().success();
    }

    @GetMapping("/{id}")
    @ResponseBody
    protected WebBaseResult<T> get(@PathVariable String id) {
        return new WebBaseResult<T>().success().data(biz.selectByPrimaryKey(id));
    }

    @PutMapping("/{id}")
    @ResponseBody
    protected WebBaseResult<T> update(T t) {
        biz.updateByPrimaryKey(t);
        return new WebBaseResult<T>().success();
    }

    @DeleteMapping("/{id}")
    @ResponseBody
    protected WebBaseResult<T> delete(@PathVariable int id) {
        biz.deleteByPrimaryKey(id);
        return new WebBaseResult<T>().success();
    }
}
